package com.meihua.baoleyi.base;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.meihua.baoleyi.net.OnLoadMoreListener;
import com.meihua.baoleyi.net.OnRefreshListener;
import com.meihua.baoleyi.view.IRecyclerView;
import com.meihua.baoleyi.view.LoadMoreFooterView;

/**
 * Created by ${zhangyuanchao}
 */

public abstract class BaseRecyclerViewActivity extends BaseActivity implements OnLoadMoreListener, OnRefreshListener {
    public LoadMoreFooterView mLoadMoreFooterView;
    private RecyclerView.Adapter mAdapter;
    public int mPage = 1;
    IRecyclerView recyclerView;

    /*初始化irecyclerview*/
    public void initRecyclerView(final IRecyclerView recyclerView) {
        this.recyclerView = recyclerView;
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        /*加载更多底部View*/
        mLoadMoreFooterView = (LoadMoreFooterView) recyclerView.getLoadMoreFooterView();
        if (mAdapter == null) {
            mAdapter = getAdapter();
            recyclerView.setIAdapter(mAdapter);
        } else {
            mAdapter.notifyDataSetChanged();
        }
        recyclerView.setOnRefreshListener(this);
        recyclerView.setOnLoadMoreListener(this);
        recyclerView.post(new Runnable() {
            @Override
            public void run() {
                getServiceData(1);
            }
        });
    }

    protected abstract RecyclerView.Adapter getAdapter();

    protected abstract void getServiceData(int mPage);

    @Override
    public void onLoadMore(View loadMoreView) {
        mPage++;
        getServiceData(mPage);
        mLoadMoreFooterView.setStatus(LoadMoreFooterView.Status.LOADING);
    }

    @Override
    public void onRefresh() {
        mPage = 1;
        recyclerView.setRefreshing(true);
        getServiceData(mPage);
        mLoadMoreFooterView.setStatus(LoadMoreFooterView.Status.GONE);
    }
}
